import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class CusStatelessWidget extends StatelessWidget {
  const CusStatelessWidget({
    Key? key,
    required this.text,
    this.backgroundColor:Colors.grey,
  }):super(key:key);

  final String text;
  final Color backgroundColor;

  @override
  Widget build(BuildContext context) {
    Scaffold? scaffold = context.findAncestorWidgetOfExactType<Scaffold>();
    // 直接返回 AppBar的title， 此处实际上是Text("Context测试")
    // var partText =(scaffold?.appBar as AppBar).title;
    return Scaffold(
      appBar: AppBar(
        title: Text(text+"----该内容可传递到下一级的widget中"),
      ),
      body: Container(
        child: Builder(builder: (context) {
          // 在Widget树中向上查找最近的父级`Scaffold` widget，当前最近的是==AppBar
          Scaffold? scaffold = context.findAncestorWidgetOfExactType<Scaffold>();
          // 直接返回 AppBar的title， 此处实际上是Text("Context测试")
          return (scaffold?.appBar as AppBar).title!;
        }),

      ),

    );
  }
}